/*******************************************************************************
																				
	DESCRIPTION:  	This do file analyses the different categories of 
					unemployment subspells (e.g., training, subsidized work, etc.)

*******************************************************************************/

clear all
global id_code 005

*******************************************************************************
 * Obtain the variable indicating the spell included a training
*******************************************************************************

* Load the data with sub-spells
use "${data}/001_1_UnemploymentSubspells", clear

* Generate a variable indicating that a sub-spell was vocational training
* (this info is from "Skat Master File" in Dropbox)
gen voc_training = 0
replace voc_training = 1 if inlist(Skat, 32, 64, 68, 69, 76, 81, 82, 84, 86, 87, 88)

* Generate a variable indicating that a sub-spell was non-vocational training
gen nonvoc_training = 0
replace nonvoc_training = 1 if inlist(Skat, 28, 71, 73, 74, 76, 80, 83)

* The following categories change over time, so need to be assigned more carefully
replace nonvoc_training = 1 if Skat==72 & searchStart>2005 
// Note: first definition used until 2001, second used from 2010. 
// Here 2005 is chosen arbitrarily, but anthing else between 2002-2009 works

* Generate a variable including both types of training
gen training_combined = 0
replace training_combined = 1 if voc_training==1 | nonvoc_training==1

*Ggenerate a variable indicating that a sub-spell was work experience
gen work_experience = 0
replace work_experience = 1 if inlist(Skat, 52, 54, 55, 59, 60, 61, 62, 63, 67)
replace work_experience = 1 if inlist(Skat, 15, 57, 70, 72, 85)

* Generate a variable indicating that a sub-spell was "workfare employment"
gen workfare = 0
replace workfare = 1 if inlist(Skat, 65, 66, 79, 99)

* The following categories change over time, so need to be assigned more carefully
replace workfare = 1 if Skat==50 & searchStart>2015
replace workfare = 1 if Skat==51 & (searchStart<2005 | searchStart>2010)
replace workfare = 1 if Skat==56 & searchStart<2005

* Generate a variable indicating that a sub-spell was subsidized work
gen subs_work_notdisabled = 0
replace subs_work_notdisabled = 1 if inlist(Skat, 30, 33, 36, 37, 40, 44, 45, ///
	47, 48, 49, 51, 53, 58, 77, 78)
	
* The following categories change over time, so need to be assigned more carefully
replace subs_work_notdisabled = 1 if Skat==50 & searchStart<2015
replace subs_work_notdisabled = 1 if Skat==58 & searchStart<2005
	
* Generate a variable indicating that a sub-spell was subsidized work for the disabled
gen subs_work_disabled = 0
replace subs_work_disabled = 1 if inlist(Skat, 38, 39, 42, 43)

* The following categories change over time, so need to be assigned more carefully
replace subs_work_disabled = 1 if Skat==56 & searchStart>2005
replace subs_work_disabled = 1 if Skat==58 & searchStart>2005
	
* Generate a variable indicating that a sub-spell was under the start-up incentive
gen startup_incentive = 0
replace startup_incentive = 1 if Skat==46
	
* Generate old definition of training
gen training_old = (Skat==52 | Skat==54 | Skat==55 | Skat==57 |					///
	(Skat>=59 & Skat<=73) | Skat==75 | (Skat>=79 & Skat<=83) 				///
	| (Skat>=85 & Skat<=88) | Skat==99)

* Generate a variable for work and workfare interventions:
gen work = 0
replace work = 1 if (work_experience==1 | workfare==1)
	
* Generate a variable for subsidies:
gen subs = 0
replace subs = 1 if (subs_work_notdisabled==1 | subs_work_disabled==1 | startup_incentive==1)
	
	
* Generate a variable indicating that a sub-spell was part of any active labor
* market policy (ALMP)
gen ALMP = 0
replace ALMP = 1 if (training_combined==1 | work_experience==1 | workfare==1 |	///
		subs_work_notdisabled==1 | subs_work_disabled==1 | startup_incentive==1)
	
* Generate a variable with the start date of the sub-spell if the sub-spell is x
foreach x in voc_training nonvoc_training training_combined ///
	work_experience workfare work ///
	subs_work_notdisabled subs_work_disabled startup_incentive subs ///
	training_old ALMP {
	gen `x'_date_temp = .
	replace `x'_date_temp = searchStart if `x'==1
	format `x'_date_temp %td
}

* For every spell generate a variable indicating the start date of the first x sub-spell
sort Lop* InLnr searchStart	
foreach x in voc_training nonvoc_training training_combined ///
	work_experience workfare work ///
	subs_work_notdisabled subs_work_disabled startup_incentive subs ///
	training_old ALMP {
	bys Lop* InLnr: egen `x'_date = min(`x'_date_temp)
	format `x'_date %td
}

* For every spell generate a variable indicating the duration of the first x sub-spell
foreach x in voc_training nonvoc_training training_combined ///
	work_experience workfare work ///
	subs_work_notdisabled subs_work_disabled startup_incentive subs ///
	training_old ALMP {
	gen `x'_duration1 = searchEnd - searchStart if `x'==1
	gen `x'_duration2 = `x'_duration1 if searchStart==`x'_date
	bys Lop* InLnr: egen `x'_duration = mean(`x'_duration2)
}

* For every spell generate a variable indicating that the spell included at least one x sub-spell	
foreach x in voc_training nonvoc_training training_combined ///
	work_experience workfare work ///
	subs_work_notdisabled subs_work_disabled startup_incentive subs ///
	training_old ALMP {
	bys Lop* InLnr: egen any_`x' = max(`x')
}

* Calculate the day of the SPELL on which the first SUB-SPELL of each category
* starts
foreach category in voc_training nonvoc_training training_combined ///
	work_experience workfare work ///
	subs_work_notdisabled subs_work_disabled startup_incentive subs ///
	training_old ALMP {
	gen `category'_start = `category'_date + 1 - startU
}

* Create a dummy variable for each category for whether or not the spell entered
* that category within its first 180 days
foreach category in voc_training nonvoc_training training_combined ///
	work_experience workfare work ///
	subs_work_notdisabled subs_work_disabled startup_incentive subs ///
	training_old ALMP {
	gen `category'_6months = inrange(`category'_start, 0, 180)

}

* Keep only relevant variables and save the dataset
keep Lop* InLnr voc_training_date any_voc_training 					 		///
	nonvoc_training_date any_nonvoc_training training_combined_date 		///
	any_training_combined work_experience_date 								///
	any_work_experience	workfare_date any_workfare 							///
	subs_work_notdisabled_date any_subs_work_notdisabled 					///
	subs_work_disabled_date any_subs_work_disabled							///
	startup_incentive_date any_startup_incentive							///
	training_old_date any_training_old ALMP_date any_ALMP		///
	any_work any_subs ///
	voc_training_duration nonvoc_training_duration 							///
	training_combined_duration work_experience_duration 					///
	workfare_duration 		work_duration											///
	subs_work_notdisabled_duration subs_work_disabled_duration 				///
	startup_incentive_duration subs_duration training_old_duration ALMP_duration					///
	voc_training_start nonvoc_training_start training_combined_start 		///
	work_experience_start workfare_start work_start subs_work_notdisabled_start		///
	subs_work_disabled_start startup_incentive_start subs_start training_old_start		///
	ALMP_start																///
	voc_training_6months nonvoc_training_6months training_combined_6months	///
	work_experience_6months workfare_6months subs_work_notdisabled_6months	///
	subs_work_disabled_6months startup_incentive_6months 					///
	training_old_6months ALMP_6months work_6months subs_6months
duplicates drop Lop* InLnr, force

save "${data}/${id_code}_UnemploymentCategoryStats", replace
